package com.cvo.scrumtoolkit.server;


import java.util.ArrayList;
import java.util.List;

import com.cvo.scrumtoolkit.client.entities.BacklogItem;
import com.cvo.scrumtoolkit.client.entities.Project;
import com.cvo.scrumtoolkit.client.entities.WorkItem;
import com.cvo.scrumtoolkit.client.pannels.PersistentService;
import com.cvo.scrumtoolkit.client.pannels.PersistentServiceAsync;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;




@SuppressWarnings("serial")

//public class BlobServiceImpl extends RemoteServiceServlet implements BlobService {
public class PersistentServiceImpl extends RemoteServiceServlet implements PersistentService {
	
	
	
  
	public void register() {
		 ObjectifyService.register(Project.class);
		 ObjectifyService.register(BacklogItem.class);
		 ObjectifyService.register(WorkItem.class);
	}	


//PROJECTEN
public void persistProject(String projectnaam, String scrummaster)
		{
	 Objectify ofy = ObjectifyService.begin();
	    
	   Project p = new Project();
	    p.setProjectname(projectnaam);
	   p.setScrummaster(scrummaster);
	   p.setAantalSprints(0);
	    
	    ofy.put(p);
	    

		
	}



public ArrayList<Project> searchProjects() {
	 Objectify ofy = ObjectifyService.begin();
	    
	    Query<Project> q = ofy.query(Project.class);
	    
	    ArrayList<Project> projects = new ArrayList<Project>();
	    
	      for (Project fetched : q) {
	      
	      projects.add(fetched);
	    }
	    
	    return projects;
	    
	  }

public int setSprintsInProject(String naam){
	
	 Objectify ofy = ObjectifyService.begin();
	    
	    Project q = ofy.query(Project.class).filter("projectname", naam).get();
	    int i= q.getAantalSprints()+1;
	    q.setAantalSprints(i);
	    ofy.put(q);
	    		
	    		return i;
}

public int getSprintsInProject(String naam){
	
	 Objectify ofy = ObjectifyService.begin();
	    
	    Project q = ofy.query(Project.class).filter("projectname", naam).get();
	    int i= q.getAantalSprints();
	    
	    		
	    		return i;
}

//BACKLOGITEMS

@Override
public void persistBacklogitem(String naam, int uur, String pid) {
	 Objectify ofy = ObjectifyService.begin();
	    
	  BacklogItem p =new BacklogItem();
	  p.setBacklogItemName(naam);
	  p.setGeschatteTijd(uur);
	  p.setProjectId(pid);
	  p.setSprintnummer(0);  
	    ofy.put(p);
	    
	
}



@Override
public List<BacklogItem> getItems(String project) throws Exception {
	 Objectify ofy = ObjectifyService.begin();
	    
	    Query<BacklogItem> q = ofy.query(BacklogItem.class).filter("projectId",project);
	    
	    ArrayList<BacklogItem> projects = new ArrayList<BacklogItem>();
	    
	      for (BacklogItem fetched : q) {
	      
	      projects.add(fetched);
	    }
	    
	    return projects;
	    
	  
}
public List<BacklogItem> getItemsinSprint(String project,int sprint) throws Exception {
	 Objectify ofy = ObjectifyService.begin();
	    
	    Query<BacklogItem> q = ofy.query(BacklogItem.class).filter("projectId",project).filter("sprintnummer", sprint);
	    
	    ArrayList<BacklogItem> projects = new ArrayList<BacklogItem>();
	    
	      for (BacklogItem fetched : q) {
	      
	      projects.add(fetched);
	    }
	    
	    return projects;
	    
	  
}
public void setSprintInBacklogitem(String naam,int i){
	 Objectify ofy = ObjectifyService.begin();
	BacklogItem q = ofy.query(BacklogItem.class).filter("name", naam).get();
	q.setSprintnummer(i);
	ofy.put(q);

}


//WORKITEMS

public void persistWorkitem(String backlogItem,String naam,String developper) {
	 Objectify ofy = ObjectifyService.begin();
	    
	 WorkItem p =new WorkItem();
	  p.setWorkitemname(naam);
	  p.setBacklogItem(backlogItem);
	  p.setDeveloper(developper);
	  p.setStatus("rood");  
	    ofy.put(p);
	    
	
}

public void setStatus(String workItem,String status){
	 Objectify ofy = ObjectifyService.begin();
	WorkItem w=ofy.query(WorkItem.class).filter("workitemname", workItem).get();
	w.setStatus(status);
	ofy.put(w);
	
}
public void setWerkuren(String workItem,int werkuren){
	 Objectify ofy = ObjectifyService.begin();
	WorkItem w=ofy.query(WorkItem.class).filter("workitemname", workItem).get();
	w.setStatus("groen");
	w.setGewerkteUren(werkuren);
	ofy.put(w);
	
}

public List<WorkItem>getWorkItems(String backlogitem){
	 Objectify ofy = ObjectifyService.begin();
	    
	    Query<WorkItem> q = ofy.query(WorkItem.class).filter("backlogItem",backlogitem);
	    
	    ArrayList<WorkItem> projects = new ArrayList<WorkItem>();
	    
	      for (WorkItem fetched : q) {
	      
	      projects.add(fetched);
	    }
	    
	    return projects;
	    
	
}


@Override
public int getNumberOfItems(Project project) throws Exception {
	// TODO Auto-generated method stub
	return 0;
}

















}


